package org.yjhking.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Service;
import org.yjhking.domain.MovieTicket;
import org.yjhking.domain.User;
import org.yjhking.dto.Movie;
import org.yjhking.feignclient.MovieFeignClient;
import org.yjhking.mapper.UserMapper;
import org.yjhking.query.AjaxResult;
import org.yjhking.service.UserService;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author YJH
 */
@Service
public class UserServiceImpl implements UserService {
    
    @Resource
    private UserMapper userMapper;
    @Resource
    private MovieFeignClient movieFeignClient;
    
    @Override
    public int deleteByPrimaryKey(Long id) {
        return userMapper.deleteByPrimaryKey(id);
    }
    
    @Override
    public int insert(User record) {
        return userMapper.insert(record);
    }
    
    @Override
    public int insertSelective(User record) {
        return userMapper.insertSelective(record);
    }
    
    @Override
    public User selectByPrimaryKey(Long id) {
        return userMapper.selectByPrimaryKey(id);
    }
    
    @Override
    public int updateByPrimaryKeySelective(User record) {
        return userMapper.updateByPrimaryKeySelective(record);
    }
    
    @Override
    public int updateByPrimaryKey(User record) {
        return userMapper.updateByPrimaryKey(record);
    }
    
    @Override
    public AjaxResult getMovieList() {
        // 调用接口查询所有电影票
        AjaxResult movieList = movieFeignClient.getMovieList();
        ObjectMapper objectMapper = new ObjectMapper();
        // 将返回的Object对象转换成List对象
        List<MovieTicket> list = objectMapper.convertValue(movieList.getResultObj(), List.class);
        return new AjaxResult(true, "查询成功", list);
    }
    
    @Override
    public AjaxResult buy(Movie movie) {
        // 调用接口购买电影票
        return movieFeignClient.buy(movie);
    }
    
    @Override
    public AjaxResult insertUser(User user) {
        // 判断用户名是否为空
        if (user.getUsername() == null || user.getUsername().trim().equals("")) {
            return new AjaxResult(false, "用户名不能为空");
        }
        // 将用户新增到数据库
        userMapper.insert(user);
        // 调用接口，免费赠票
        return movieFeignClient.free(user.getId());
    }
    
    @Override
    public AjaxResult watchMovie(Movie movie) {
        // 空值校验
        if (movie.getUserId() == null || movie.getMovieId() == null) {
            return new AjaxResult(false, "用户id和电影id不能为空");
        }
        // 从数据库查询用户信息
        User user = userMapper.selectByPrimaryKey(movie.getUserId());
        // 判断用户是否存在
        if (user == null) {
            return new AjaxResult(false, "用户不存在");
        }
        // 调用接口观看电影
        return movieFeignClient.watchMovie(movie);
    }
    
    @Override
    public AjaxResult getUserById(Long id) {
        return new AjaxResult(true, "查询成功", userMapper.selectByPrimaryKey(id));
    }
    
    @Override
    public AjaxResult updateUser(User user) {
        return new AjaxResult(true, "更新成功", updateByPrimaryKeySelective(user));
    }
}
